package mianyang.zhangsan.com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Notice;
import mianyang.zhangsan.com.service.NoticeService;
import mianyang.zhangsan.com.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 前台查询新闻信息
     * @param pageNo
     * @param model
     * @return
     */
    @RequestMapping("/frontList")
    public String frontList(@RequestParam("pageNo")int pageNo, Model model){
        Page<Notice> noticePage = noticeService.queryPage(pageNo);
        model.addAttribute("noticeList", noticePage.getRecords());
        noticePage.setRecords(null);
        model.addAttribute("page", noticePage);
        model.addAttribute("pages", noticePage.getPages());
        return "notice/notice_front_result";
    }

    /**
     * 后台跳转新闻发布页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String noticeToAdd(){
        return "notice/notice_add";
    }

    /**
     * 后台跳转新闻查询页面
     * @return
     */
    @RequestMapping("/query")
    public String roomAdd(){
        return "notice/notice_query_result";
    }

    /**
     * 后台发布新闻
     * @param notice
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String noticeAdd(Notice notice){
        if (notice != null){
            noticeService.addNotice(notice);
            return "{\"success\":true}";
        }
        return "{\"success\":false}";
    }

    /**
     * 后台查询新闻请求
     * @param page     当前页码
     * @param rows     分页大小
     * @param notice  查询条件
     * @return
     */
    @RequestMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String noticeList(int page, int rows, Notice notice){
        System.out.println(notice);
        Page<Notice> noticePage = noticeService.queryPageWrapper(page, rows, notice);
        JSONObject response = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        response.put("total", noticePage.getPages());
        for (Notice item:noticePage.getRecords()){
            Object o = JSON.toJSON(item);
            jsonArray.add(o);
        }
        response.put("rows", jsonArray);
        return response.toString();
    }

    /**
     * 后台删除新闻信息
     * @param noticeIds
     * @return
     */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deletes(String noticeIds){
        noticeService.deletes(noticeIds);
        return "{\"success\":true}";
    }

    /**
     * 后台更新新闻消息
     * @param noticeId
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(int noticeId, Notice notice){
        noticeService.updateNotice(noticeId, notice);
        return "{\"success\":true}";
    }

    /**
     * 后台导出新闻信息
     * @param notice
     * @param response
     */
    @RequestMapping("/outToExcel")
    public void toExcel(Notice notice, HttpServletResponse response){
        List<Notice> notices = noticeService.queryListByWrapper(notice);
        String[] headers = {"序号", "标题", "内容", "点赞数", "发布时间"};
        String title = "新闻表";
        List<String[]> contents = new ArrayList<>();
        for (Notice item : notices){
            contents.add(new String[]{item.getNoticeId().toString(),item.getTitle(),item.getContent(),item.getHit().toString(),item.getPublishDate()});
        }
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setHeader("Content-disposition","attachment; filename="+"notice.xls");//filename是下载的xls的名，建议最好用英文
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma","No-cache");//设置头
            response.setHeader("Cache-Control","no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            ExcelUtils.exportExcel(title, headers, contents, out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
